//
//  OCRModule.m
//  OCRModule
//
//  Created by 鲁利杰 on 2018/1/5.
//  Copyright © 2018年 鲁利杰. All rights reserved.
//

#import "OCRModule.h"
#import "OCRModuleView.h"
// System Library
#import <Photos/Photos.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <CoreLocation/CLLocationManager.h>
// Third party Library
#import <AipOcrSdk/AipOcrSdk.h>

@interface OCRModule()
@property (nonatomic, copy) RCTPromiseResolveBlock resolve;
@property (nonatomic, copy) RCTPromiseRejectBlock reject;
@end

@implementation OCRModule

RCT_EXPORT_MODULE(Ocr)

RCT_EXPORT_METHOD(ocrInit:(NSString *)ak
                    andSK:(NSString *)sk
                  resolve:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject) {
    _resolve = resolve;
    _reject  = reject;
    [[AipOcrService shardService] authWithAK:ak andSK:sk];
    _resolve(@"成功授权");
}

RCT_EXPORT_METHOD(getOcrMessage:(NSString *)path
                        resolve:(RCTPromiseResolveBlock)resolve
                       rejecter:(RCTPromiseRejectBlock)reject) {
    
    OCRModuleView *ocrView = [[OCRModuleView alloc] initOcrModule:resolve reject:reject imagePath:path];
    UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    [rootVC.view addSubview:ocrView];

}
RCT_EXPORT_METHOD(getPhotoPath:(RCTPromiseResolveBlock)resolve
                      rejecter:(RCTPromiseRejectBlock)reject) {
    _resolve = resolve;
    _reject  = reject;
    if ([self authPhotoAlert]) {
        [self getOriginalImages];
    } else {
        NSError *error = [NSError errorWithDomain:[NSString stringWithFormat:@"%s", __FUNCTION__] code:0 userInfo:nil];
        _reject(@"authPhotoAlert", @"Access to albums permissions failed", error);
    }
}
- (void)getOriginalImages {
    NSMutableArray *photos = [NSMutableArray arrayWithCapacity:0];
    PHFetchResult<PHAsset *> *assets = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
    
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    // 同步获得图片, 只会返回1张图片
    options.synchronous = YES;
    
    for (PHAsset *asset in assets) {
        // 获取原图
        CGSize size = CGSizeMake(asset.pixelWidth, asset.pixelHeight);
        
        // 从asset中获得图片
        [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            
            NSString *filePath = [info valueForKey:@"PHImageFileURLKey"];
            NSLog(@"%@=%@=%@", result, info, filePath);
            [photos addObject:filePath];
        }];
    }
    _resolve(photos);

}
- (BOOL)authPhotoAlert {
    
    ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
    
    if (author == kCLAuthorizationStatusRestricted || author == kCLAuthorizationStatusDenied)
    {
        return NO;
        
    } else {
        
        return YES;
    }
    
}
- (dispatch_queue_t)methodQueue {
    return dispatch_get_main_queue();
}
@end
